define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kzvote/gift/order/index' + location.search,
                    // add_url: 'kzvote/gift/order/add',
                    // edit_url: 'kzvote/gift/order/edit',
                    // del_url: 'kzvote/gift/order/del',
                    // multi_url: 'kzvote/gift/order/multi',
                    // import_url: 'kzvote/gift/order/import',
                    table: 'kzvote_gift_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                showExport: false,//隐藏导出
                showToggle: false,//隐藏浏览模式
                showColumns: false,//隐藏显示隐藏模式
                searchFormVisible: false,//默认显示搜索表单
                search: false,//默认显示搜索表单
                columns: [
                    [
                        // {field: 'id', title: __('Id')},
                        // {field: 'vote_id', title: __('Vote_id'),visible:false},
                        {field: 'vote.name', title: __('投票活动'), operate: 'LIKE'},

                        // {field: 'person_id', title: __('Person_id'),visible:false},
                        {field: 'person.name', title: __('作品'), operate: 'LIKE'},

                        {field: 'user_id', title: __('User_id'),visible:false, operate: false},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},

                        {field: 'gift_id', title: __('Gift_id'),visible:false, operate: false},

                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'money', title: __('Money'), operate:false},
                        {field: 'num', title: __('Num'), operate:false},
                        // {field: 'order_no', title: __('Order_no'),visible:false, operate: false},
                        {field: 'status', title: __('Status'), searchList: {"10":__('Status 10'),"20":__('Status 20')}, formatter: Table.api.formatter.status},
                        {field: 'type', title: __('Type'), searchList: {1:__('Type 1'),2:__('Type 2')}, formatter: Table.api.formatter.status},

                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                $("#money").text(data.money);
            });

            $('.btn-delunpay').click(function (){
                Layer.confirm(
                    "确定删除所有未付款订单吗？",
                    {icon: 3, title: __('Warning'), shadeClose: true},
                    function (index) {

                        Fast.api.ajax({
                            url: 'kzvote/gift.order/delunpay',
                        }, function(data, ret){
                            Toastr.success(ret.msg);
                            $('.btn-refresh').click();
                            return false;
                        }, function(data, ret){
                            Toastr.error(ret.msg);
                            return false;
                        });

                        Layer.close(index);
                    }
                );
            });


        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        give: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
